package com.atguigu.gulimall.search.controller;

import com.atguigu.gulimall.search.service.MallSearchService;
import com.atguigu.gulimall.search.vo.SearchParam;
import com.atguigu.gulimall.search.vo.SearchResult;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
@Slf4j
public class SearchController {

    @Autowired
    ObjectMapper objectMapper;

    @Autowired
    MallSearchService mallSearchService;

    @SneakyThrows
    @GetMapping("/list.html")
    public String list(SearchParam searchParam, Model model, HttpServletRequest httpServletRequest) {
        /*
        localhost:12000/list.html
keyword,华为
catalog3Id,225
sort,hotScore_desc
hasStock,1
skuPrice,1000_9999
brandId,1
attrs,1_543
pageNum,1
         */
        String queryString = httpServletRequest.getQueryString();
        searchParam.setQueryString(queryString);
        SearchResult result = mallSearchService.search(searchParam);
        model.addAttribute("result", result);
//        log.info("SearchResult: {}", result);
        return "list";
    }
}
